/*
 * date: 2009-11-20 
 * author: Maciej Smolenski <jezdnia@gmail.com>
 */
#include "rpc_monitor_data.hpp"

namespace rpc_monitor
{

rpc_service_string::rpc_service_string(const rpc_service_shm_string &rs) : rpc_service<std::string>::rpc_service(rs.address.c_str(), rs.program, rs.version, rs.port, rs.protocol, rs.state) {};

rpc_service_shm_string::rpc_service_shm_string(const rpc_service_string &rs, shm_char_allocator &al) : rpc_service<shm_string>::rpc_service(shm_string(rs.address.c_str(), al), rs.program, rs.version, rs.port, rs.protocol, rs.state) {};

rpc_service_string::rpc_service_string(const std::string p_add, const long unsigned p_program, const long unsigned p_version, const long unsigned p_port, const protocol_t p_protocol, const state_t p_state) : rpc_service<std::string>(p_add, p_program, p_version, p_port, p_protocol, p_state) {}; 

rpc_service_shm_string::rpc_service_shm_string(const shm_string p_add, const long unsigned p_program, const long unsigned p_version, const long unsigned p_port, const protocol_t p_protocol, const state_t p_state) : rpc_service<shm_string>(p_add, p_program, p_version, p_port, p_protocol, p_state) {};

result_t std_set_to_bi_set(bi::managed_shared_memory &segment, const stdset &si, biset &so)
{
	shm_char_allocator c_allocator(segment.get_segment_manager());

	so.clear();
	for(stdset::const_iterator si_it=si.begin(); si_it!=si.end(); si_it++)
		so.insert(rpc_service_shm_string(*si_it, c_allocator));
	return result_success;
}

result_t bi_set_to_std_set(bi::managed_shared_memory &segment, const biset &si, stdset &so)
{
	so.clear();
	for(biset::const_iterator si_it=si.begin(); si_it!=si.end(); si_it++)
		so.insert(rpc_service_string(*si_it));
	return result_success;
}

result_t rpc_monitor::init(bi::managed_shared_memory &m_segment)
{
	shm_rpc_service_shm_string_allocator s_allocator(m_segment.get_segment_manager());
	m_services = m_segment.construct<biset>(bi::anonymous_instance)(std::less<rpc_service_shm_string>(), s_allocator);
	return result_success;
}

result_t rpc_monitor::deinit(bi::managed_shared_memory &m_segment)
{
	m_services->clear();
	m_segment.destroy_ptr(m_services.get());
	return result_success;
}

std::ostream &operator<<(std::ostream &os, const rpc_service_string &r)
{
	os << " address: " << r.address;
	os << " program: " << r.program;
	os << " version: " << r.version;
	os << " port: " << r.port;
	os << " protocol: " << r.protocol;
	os << " state: " << r.state;
	return os;
}

}
